﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;
using Yipusoft.SixAcquaint.BLL;

public partial class BasicBase_KeyUnit_KeyUnitStat : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindDepartment("201200", "201201", "201211");
        }
    }

    private void BindDepartment(string permissionAdmin, string permissionLevel, string permissionUnit)
    {
        if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionAdmin))
        {
            gvKeyUnits.DataSource = Department.GetList("RIGHT(DepartmentID, 2) = '00'");

        }
        else
        {
            gvKeyUnits.DataSource = GetDepartmentList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID, permissionLevel, permissionUnit);

        }
        gvKeyUnits.DataBind();
    }

    private List<DepartmentInfo> GetDepartmentList(string departmentID, string permissionLevel, string permissionUnit)
    {
        List<DepartmentInfo> departmentList = new List<DepartmentInfo>();
        if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionLevel) || Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionUnit))
            departmentList = Department.GetList("DepartmentID='" + departmentID.Substring(0, 4) + "000'");
        return departmentList;
    }
    protected void gvKeyUnits_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            string departmentID = gvKeyUnits.DataKeys[e.Row.RowIndex].Values[0].ToString();
            int leftCount = 0;
            HyperLink lblCount = e.Row.FindControl("lblCount") as HyperLink;
            if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "201200"))
            {
                string where = "Left(DepartmentID,4)='" + departmentID.Substring(0, 4) + "'";
                lblCount.Text = KeyUnit.GetKeyUnitCountByWhere(where).ToString();
                leftCount = 4;

                lblCount.NavigateUrl = "SearchKeyUnit.aspx?Where=" + where;
                //soldier.GetSoldiersCountByWhere("IsDelete=0 and IsContract=0 and Left(DepartmentID,4)='" + departmentID.Substring(0, 4) + "'").ToString();
            }
            else
            {
                string where = "DepartmentID='" + departmentID + "'";
                lblCount.Text = KeyUnit.GetKeyUnitCountByWhere(where).ToString();
                lblCount.NavigateUrl = "SearchKeyUnit.aspx?Where=" + where;
                leftCount = 7;
            }
            //lblCount.Text = "0";
            Label lblSetTime = e.Row.FindControl("lblSetTime") as Label;
            if (lblCount.Text == "0")
                lblSetTime.Text = "--";
            else
                lblSetTime.Text = KeyUnit.GetLastTime(departmentID, leftCount).ToString("yyyy-MM-dd HH:mm");

        }
    }
}
